﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class ChangePlanePanel : MonoBehaviour
{
    public GameObject[] objs;
    public Button AddButton, ReduceButton, SureButton;
    public GameObject player, zhezhao;
    int Third, Fourth;
    // Start is called before the first frame update
    void Start()
    {
        objs = GameObject.FindGameObjectsWithTag("Plane").OrderBy(g => g.transform.GetSiblingIndex()).ToArray();
        player = GameObject.Find("Player");
        zhezhao = MainPanel.instance.ZheZhaoImage;
        AddButton.onClick.AddListener(OnClickAddButton);
        ReduceButton.onClick.AddListener(OnClickReduceButton);
        SureButton.onClick.AddListener(OnClickSureButton);
        Third = DataManager.Instance.HaveThird;
        Debug.Log("Third==" + Third);
        Fourth = DataManager.Instance.HaveFourth;
        Debug.Log("Fourth==" + Fourth);
    }

    // Update is called once per frame
    void Update()
    {
        HaveThird();


    }
    /// <summary>
    /// 是否拥有飞机
    /// </summary>
    public void HaveThird()
    {

        if (Third == 0 && objs[2].GetComponent<Image>().enabled == true)
        {
            SureButton.interactable = false;
        }
        else if (Fourth == 0 && objs[3].GetComponent<Image>().enabled == true)
        {
            SureButton.interactable = false;
        }

        else
        {
            SureButton.interactable = true;
        }
    }


    /// <summary>
    /// 点击右按钮
    /// </summary>
    public void OnClickAddButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        for (int i = 0; i < objs.Length; i++)
        {
            if (objs[objs.Length - 1].GetComponent<Image>().enabled == false)
            {
                if (objs[i].GetComponent<Image>().enabled == true)
                {
                    objs[i].GetComponent<Image>().enabled = false;
                    objs[i + 1].GetComponent<Image>().enabled = true;

                    break;

                }
            }
        }



    }
    /// <summary>
    /// 点击左按钮
    /// </summary>
    public void OnClickReduceButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        for (int i = 0; i < objs.Length; i++)
        {
            if (objs[0].GetComponent<Image>().enabled == false)
            {
                if (objs[i].GetComponent<Image>().enabled == true)
                {
                    objs[i].GetComponent<Image>().enabled = false;
                    objs[i - 1].GetComponent<Image>().enabled = true;

                    break;

                }
            }
        }



    }
    /// <summary>
    /// 点击确定按钮
    /// </summary>
    public void OnClickSureButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        Destroy(gameObject);
        for (int i = 0; i < objs.Length; i++)
        {
            if (objs[i].GetComponent<Image>().enabled == true)
            {
                player.GetComponent<Image>().sprite = objs[i].GetComponent<Image>().sprite;

            }
        }
        zhezhao.GetComponent<Image>().color = new Color(0, 0, 0, 0);
        zhezhao.GetComponent<Image>().raycastTarget = false;

    }
}
